package cn.framelife.rainbow.ui.wizard;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;

import cn.framelife.rainbow.ui.dialog.JavaPackageDialog;
import cn.framelife.rainbow.ui.dialog.JavaSourceFolderDialog;
import cn.framelife.rainbow.ui.dialog.NewModuleDialog;
import cn.framelife.rainbow.ui.entity.Module;
import cn.framelife.rainbow.ui.entity.ReverseProperty;
import cn.framelife.rainbow.ui.entity.WizardMessage;
import cn.framelife.rainbow.ui.provider.ModulePageProvicer;
import cn.framelife.rainbow.util.ProjectUtil;
import cn.framelife.rainbow.util.StringUtil;

import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Tree;

/**
 * Reverse Wizard Second Page
 * 主要是填写页面相关的属性
 * @ClassName: ReverseSecondPage 
 * @author 潘广伟  p_3er@qq.com 
 * @date 2013年11月21日 下午3:43:24 
 *
 */
public class ModulePage extends WizardPage {
	private Text srcFolderField;
	private Text packageField;
	private Button srcFolderButton;
	private Button packageButton;
	
	private Button btnNew;
	private Button btnAdd;
	private Button btnRemove;
	
	private Tree moduleTree;
	private Tree tableTree;
	
	private ModulePageProvicer provicer;
	private ReverseProperty reverseProperty;
	
	/**
	 * Create the wizard.
	 */
	public ModulePage(WizardMessage message,ReverseProperty reverseProperty) {
		super(message.getPageTitle());
		setTitle(message.getPageTitle());
		setDescription(message.getPageDescription());
		this.reverseProperty = reverseProperty;
		this.provicer = new ModulePageProvicer();
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);

		initUI(container);
		events();
	}
	
	public void reflesh() throws Exception{
		provicer.loadTables(tableTree);
	}
	
	/**
	 * 获取模块及表
	 *
	 * @return
	 */
	public List<Module> getModuleData(){
		List<Module> modules = new ArrayList<Module>();
		
		for(TreeItem parent :  moduleTree.getItems()){
			Module module = (Module) parent.getData();
			
			if(parent.getItemCount() > 0){
				List<String> tables = new ArrayList<String>();
				
				for(TreeItem item : parent.getItems()){
					tables.add(item.getText());
				}
				module.setTableNames(tables);
				modules.add(module);
			}
			
		}
		
		return modules;
	}

	/**
	 * 控件事件
	 *
	 * @Title: events 
	 * @param    
	 * @return void   
	 * @throws
	 */
	private void events(){
		
		srcFolderButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					IProject[] projects = ProjectUtil.getWebProjects();
					JavaSourceFolderDialog dialog = new JavaSourceFolderDialog(
							getShell(), projects);
					if (dialog.open() == Window.OK) {
						reverseProperty.setSelectionProjectName(dialog
								.getSelectionName());
						srcFolderField.setText("/"
								+ reverseProperty.getSelectionProjectName()
								+ "/src");
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				
			}
		});
		
		packageButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					JavaPackageDialog dialog = new JavaPackageDialog(
							getShell(), reverseProperty.getSelectionProjectName());
					if (dialog.open() == Window.OK) {
						packageField.setText(dialog.getSelectionName());
						
						moduleTree.removeAll();
						provicer.loadModules(moduleTree, reverseProperty.getSelectionProjectName(), dialog.getSelectionName());
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				
			}
		});
		
		packageField.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				reverseProperty.setPackageURL(packageField.getText());
			}

		});
		
		btnNew.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					if(packageField.getText().trim().length() > 0){
						NewModuleDialog dialog = new NewModuleDialog(getShell());
						if (dialog.open() == Window.OK) {
							provicer.addModuleItem(moduleTree, dialog.getModule());
						}
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				
			}
		});
		
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if( moduleTree.getSelectionCount() > 0){
					TreeItem selectedModule = moduleTree.getSelection()[0];
					List<TreeItem> checkedItems = new ArrayList<TreeItem>();
					
					for(TreeItem item :  tableTree.getItems()){
						if(item.getChecked()){
							checkedItems.add(item);
						}
					}
					for(TreeItem table : checkedItems){
						TreeItem item = new TreeItem(selectedModule, SWT.NONE | SWT.CHECK);
						item.setText(table.getText());
						table.dispose();
					}
					
				}
			}
		});
		
		btnRemove.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				List<TreeItem> checkedItems = new ArrayList<TreeItem>();
				
				for(TreeItem parent :  moduleTree.getItems()){
					for(TreeItem item : parent.getItems()){
						if(item.getChecked()){
							checkedItems.add(item);
						}
					}
				}
				
				for(TreeItem table : checkedItems){
					TreeItem item = new TreeItem(tableTree, SWT.NONE);
					item.setText(table.getText());
					table.dispose();
				}
			}
		});
	}
	
	/**
	 * 初始化UI,由工具生成(没有重构)
	 * @param container
	 */
	private void initUI(Composite container) {
		container.setLayout(new GridLayout(4, false));
		

		Label srcFolderLabel = new Label(container, SWT.NONE);
		srcFolderLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		srcFolderLabel.setText("Java src folder: ");
		
		srcFolderField = new Text(container, SWT.BORDER);
		GridData gd_srcFolderField = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_srcFolderField.widthHint = 395;
		srcFolderField.setLayoutData(gd_srcFolderField);
		srcFolderField.setEditable(false);
		initSrcFolderField();
		
		srcFolderButton = new Button(container, SWT.NONE);
		srcFolderButton.setText("Browse...");
		new Label(container, SWT.NONE);
		
		
		Label packageLabel = new Label(container, SWT.NONE);
		packageLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		packageLabel.setText("Main package: ");
		
		packageField = new Text(container, SWT.BORDER);
		packageField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		packageButton = new Button(container, SWT.NONE);
		packageButton.setText("Browse...");
		new Label(container, SWT.NONE);
		
		Label sparator_1 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_sparator_1 = new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1);
		gd_sparator_1.widthHint = 589;
		sparator_1.setLayoutData(gd_sparator_1);
		
		Composite composite = new Composite(container, SWT.NULL);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1);
		gd_composite.heightHint = 370;
		gd_composite.widthHint = 676;
		composite.setLayoutData(gd_composite);
		
		Label lblTable = new Label(composite, SWT.NONE);
		lblTable.setText("Tables:");
		new Label(composite, SWT.NONE);
		Label lblModule = new Label(composite, SWT.NONE);
		lblModule.setText("Modules:");
		
		ScrolledComposite scrolledComposite_1 = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_scrolledComposite_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_scrolledComposite_1.heightHint = 315;
		gd_scrolledComposite_1.widthHint = 200;
		scrolledComposite_1.setLayoutData(gd_scrolledComposite_1);
		scrolledComposite_1.setExpandHorizontal(true);
		scrolledComposite_1.setExpandVertical(true);
		
		tableTree = new Tree(scrolledComposite_1, SWT.CHECK | SWT.MULTI);
		scrolledComposite_1.setContent(tableTree);
		scrolledComposite_1.setMinSize(tableTree.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(1, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		btnNew = new Button(composite_1, SWT.NONE);
		btnNew.setText("New Module");
		
		btnAdd = new Button(composite_1, SWT.NONE);
		btnAdd.setText("Add Table");
		
		btnRemove = new Button(composite_1, SWT.NONE);
		btnRemove.setText("Remove Table");
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		GridData gd_scrolledComposite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_scrolledComposite.heightHint = 315;
		gd_scrolledComposite.widthHint = 200;
		scrolledComposite.setLayoutData(gd_scrolledComposite);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
		
		moduleTree = new Tree(scrolledComposite, SWT.CHECK);
		scrolledComposite.setContent(moduleTree);
		scrolledComposite.setMinSize(moduleTree.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	}
	
	/**
	 * 初始化srcFolderField的值
	 *
	 * @Title: initSrcFolderField 
	 * @param   
	 * @return void  
	 * @throws
	 */
	private void initSrcFolderField(){
		IProject project = ProjectUtil.getCurrentProject();
		if(project != null){
			srcFolderField.setText(project.getName()+"//src");
		}
	}
	
	/**
	 * 是否允许确定
	 *
	 * @Title: isFinished 
	 * @param   
	 * @return void  
	 * @throws
	 */
	public boolean isComfirm(){
		return StringUtil.isNotEmpty(srcFolderField.getText()) && StringUtil.isNotEmpty(packageField.getText());
	}
}
